package com.example.demo.cron;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/task")
@Slf4j
public class TaskController {

    @Autowired
    private DynamicTimedTask timedTask;

    @PostMapping("/time")
    public void scheduled(String cron, String msg) {
        timedTask.start(() -> {
            log.info("输出消息【{}】表达式【{}】", msg, cron);
        }, cron);
    }

    @PostMapping("/task")
    public void task(String name, String cron) {
        CustomizeTask task = new CustomizeTask(name, cron);
        timedTask.startCron(task, name, cron);
    }

    @PostMapping("/stop")
    public void stop(String name) {
        timedTask.stop(name);
    }

    @PostMapping("/stopAll")
    public void stopAll() {
        timedTask.stopAll();
    }
}